<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$this->types=array('普通卡包','特殊卡包','线上卡包');
$this->batchtypes=array('归属平台','归属门店','归属员工');
$this->jiangtype=array('送商品','送优惠券','送余额红包','送虚拟币');
$this->codestatus=array('待发卡','已领卡','已兑换');
$this->orderstatus=array('待兑换','已部分兑换','已兑换待使用','已兑换待发货','已发货','已完成','待支付','已被赠送','已申请退费','退款成功');
$this->songlog_status=array('待领取','已领取','已过期');
$tabs=array(
	array('op'=>'list','title'=>'卡包管理'),
	array('op'=>'edit','title'=>'添加卡包'),
	array('op'=>'order','title'=>'卡包订单'),
	array('op'=>'links','title'=>'入口地址'),
);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	if(!empty($_RQ['code'])){
		$params['code']=trim($_RQ['code']);
		$return['params']['code']=trim($_RQ['code']);
	}
    $fields=array();
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_goods_package',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['type']=$this->types;		
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：【线上卡包】才支持线上领取，线上购买！','list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'price','title'=>'价值','type'=>'string'),
			array('name'=>'buyprice','title'=>'售价','type'=>'string'),
			array('name'=>'times','title'=>'N次卡','type'=>'unitstring','unit'=>'次'),
		),
		'action'=>array(
		)
		),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
			array('name'=>'code','width'=>15,'title'=>'卡包编号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_goods_package',array('id'=>$id));
	}else{
		$item=array('times'=>1);
	}
	$item['goodsids']=json_decode($item['goodsids'],true);
	$item['images']=json_decode($item['images'],true);
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'模板设置',
		'tabs'=>$tabs,
		'edit'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'price','title'=>'卡包价值','type'=>'unitstring','unit'=>'元'),
			array('name'=>'times','title'=>'N次卡','type'=>'unitstring','unit'=>'次'),
			array('name'=>'buyprice','title'=>'线上售价','type'=>'unitstring','unit'=>'元'),
			array('name'=>'images','title'=>'幻灯片','type'=>'images'),
			array('type'=>'choose_many','name'=>'goodsids','title'=>'包含商品','action'=>'/goods.get_goods/many/','choose_title'=>'选择参与的商品','help'=>''),
			array('name'=>'desc','title'=>'卡包描述','type'=>'text'),
			array('name'=>'content','title'=>'卡包内容','type'=>'richtext'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title'] || !$postdata['image'] || !$postdata['times']){
		exi('名称,图片,N次卡,不能留空！','error');
	}
	$data=array_fields(array('title','price','buyprice','times','image','content','desc','status'),$postdata);
	$data['goodsids']=ijson_encode($postdata['goodsids']);
	$data['images']=ijson_encode($postdata['images']);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_goods_package',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_weishop_goods_package',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='order'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['listid'])){
		$params['listid']=trim($_RQ['listid']);
		$return['params']['listid']=$_RQ['listid'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','mid','listid','payfee','total','lastnum','createtime','orderid','type','ordersn','remark','username','mobile','selfnumber','status');
	$list=pdo_getslice('jy_weishop_goods_packageorder',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			if($item['listid']){
				$item['title'] = pdo_getcolumn('jy_weishop_goods_package',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['listid']),'title');
			}else{
				$item['title']=$item['title']?:'DIY定制礼包';
			}
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['id']=$item['orderid'];
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['awardstatus'] =$this->awardstatus;	
	$return['htmls']=array(
		  'type' => 'list',
		  'tabs'=>$tabs,
		  'list' => array('data' => 'list','tip'=>'客户购买卡包后，点击提取商品，会产生客户商城订单！','delall' => false,'del_one' => false,'edit' => false,'list' => array(
				array('name'=>'member','title'=>'客户','type'=>'member'),
				array('name' => 'ordersn','type' => 'string','title' => '订单编号',),
				array('name' => 'title','type' => 'string','title' => '卡包名称',),
				array('name' => 'payfee','type' => 'string','title' => '付费',),
				array('name' => 'total','type' => 'string','title' => '份数',),
				array('name' => 'lastnum','type' => 'string','title' => '剩余份数',),
				array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'orderstatus'),
				array('name'=>'createtime','title'=>'创建时间','type'=>'time'),    
			 ),
			 'action'=>array(
				array('type'=>'href','action'=>'jy_weishop/order.detail/','title'=>'查看商品订单','show'=>array('orderid >'=>0)),
			)),
		 'search'=>array(
			array('type'=>'select','width'=>15,'name'=>'listid','title'=>'所属卡包','options'=>'kabaos'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'orderstatus'),
		)
	);
	$return['orderstatus']=$this->orderstatus;
	$return['kabaos']=pdo_getall('jy_weishop_goods_package',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	exi($return);
}
if($_RQ['op']=='links'){
	$item['index']='jy_weishop/sale.kabao/';
	$item['index2']=custom_url('jy_weishop/sale.kabao/',true);
	$item['orderlist']='jy_weishop/sale.kabaoorder/';
	$item['orderlist2']=custom_url('jy_weishop/sale.kabaoorder/',true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'desc'=>'入口链接',
		'tabs'=>$tabs,
		'look'=>array(
			array('type'=>'looklink','title'=>'卡包入口','name'=>'index'),
			array('type'=>'ewm','title'=>'卡包入口','name'=>'index2'),
			array('type'=>'looklink','title'=>'我的卡包','name'=>'orderlist'),
			array('type'=>'ewm','title'=>'我的卡包','name'=>'orderlist2'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_goods_package',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
?>